package com.example.weather_app.myadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.weather_app.R;

import java.util.List;
import java.util.zip.Inflater;

public class DeleteCityAdapter extends BaseAdapter {

    private Context context;
    private List<String> cityOld;
    private List<String> cityNew;

    public DeleteCityAdapter(Context context, List<String> cityOld, List<String> cityNew) {
        this.context = context;
        this.cityOld = cityOld;
        this.cityNew = cityNew;
    }

    @Override
    public int getCount() {
        return cityOld.size();
    }

    @Override
    public Object getItem(int i) {
        return cityOld.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder holder = null;
        if (view==null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_deletecity, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }
        String s = cityOld.get(i);
        holder.tv.setText(s);
        //预删除存储,设置监听
        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cityOld.remove(s);
                cityNew.add(s);
                //适配器更新
                notifyDataSetChanged();
            }
        });
        return view;
    }

    //Holder
    class ViewHolder{
        private TextView tv;
        private ImageView iv;
        public ViewHolder(View itemView) {
            tv = itemView.findViewById(R.id.item_delete_tv);
            iv = itemView.findViewById(R.id.item_delete_iv);
        }
    }
}
